<?php
	class Properties extends MY_Controller{	
		function Properties() {
			parent::__construct();
			@session_start();
			$this->mod_ad->is_logged_in();	
			$_SESSION['redirect'] = $this->uri->uri_string();	
		}

		function index(){
			$data['title'] = "Thuộc tính sản phẩm";
			$data['content_admin'] = 'back_end/properties/index';
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			$data['table'] = 'properties';
			$data['controller'] = 'properties';
			$data['vl_sort'] = "DESC";	
			$data['sort'] = 'ASC';
			
			$per_page = 30;
			$uri = 4;
			$data['admin_properties'] = $this->mod_lib->load_all('properties','',array(),$per_page,$this->uri->segment($uri),array("id"=>"desc"));																													
			$config['base_url']     =  site_url('back_end/properties/index/');
			$config['total_rows']   =  $this->db->count_all('properties');
			$config['per_page']     =  $per_page; 
			$config['uri_segment']  =  $uri; 
			$this->pagination->initialize($config);
			$this->load->view('back_end/template_backend',$data);
		} 
		
		
		function search(){
			$data['vl_sort'] = "DESC";	
			$data['sort'] = 'ASC';
			$data['table'] = 'properties';
			$check_search = $this->input->POST('formSearch');
			$per_page = 30;
			$data['title'] = "Tìm kiếm - Tin tức";
			$data['controller'] = 'properties';
			$data['list_categoryproperties'] = $this->db->get('category_properties')->result_array();
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			$data['content_admin'] = 'back_end/properties/index';
						
			$where = array(
				'category_properties_id' => $this->input->POST('parentSearch')
			);
			
			if(!empty($check_search)){
				$_SESSION['like'] = array('title' =>$this->input->POST('nameSearch'));																												
			}
			
			$data_search = $this->mod_lib->search('properties','',$_SESSION['like'],$where,array("id"=>"desc"));
			$data['admin_properties'] = $this->mod_lib->load_search('properties','',$_SESSION['like'],$where,$per_page,$this->uri->segment(4),array("id"=>"desc"));		
			$config['base_url']     =  site_url('back_end/properties/search/');
			$config['total_rows']   =  count($data_search);
			$config['per_page']     =  $per_page;
			$config['uri_segment']  =  4; 
			$this->pagination->initialize($config);		
			$this->load->view('back_end/template_backend',$data);	
		}
		
		function sort($field,$sort = ''){
			$per_page = 30;
			$uri = 4;
			$data['title'] = "Sắp xếp - tin";
			$data['table'] = 'properties';
			$data['controller'] = 'properties';
			$data['content_admin'] = 'back_end/properties/index';
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			$_SESSION['sort'] = $sort;
			$_SESSION['field'] = $field;
			
			if ($sort == 'DESC') {
				$data['sort'] =  'ASC';
			} else if($sort == 'ASC') {
				$data['sort'] =  'DESC';
			}
			
			$data_search = $this->mod_lib->search('properties','',array(),array(),array("id"=>"desc"));
			$data['admin_properties'] = $this->mod_lib->load_search('properties','',array(),array(),$per_page,$this->uri->segment($uri),array($_SESSION['field']=>$_SESSION['sort']));		
			$config['base_url']     =  site_url('back_end/properties/index');

			$config['total_rows']   =  count($data_search);
			$config['per_page']     =  $per_page;
			$config['uri_segment']  =  $uri; 
			$this->pagination->initialize($config);		
			$this->load->view('back_end/template_backend',$data);
		}
		
		
		function add(){
			$data['title']			= "Thêm thuộc tính";
			$data['content_admin'] = 'back_end/properties/add';
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			$add = $this->input->post('addForm');
			$ctg = $this->db->get('category_product')->result_array();
		
			if(!empty($add)){
				$data_value = array (
								'name'						=> trim($this->input->post('title')),
								'description'       		=> $this->input->post('description'),
								'status'     				=> trim($this->input->post('status')),	
							);
							
				if($this->mod_lib->insert('properties',$data_value )){
					$id = $this->db->insert_id();
					
					foreach ($ctg as $ctg_p) {
						$data_properties  = array(
							'properties_id'  => $id,
							'product_id'  => $ctg_p['id']	
						);
						
						$this->mod_lib->insert('product_properties',$data_properties);						
					}
					$this->session->set_flashdata('msview', 'Thêm thành công !');
					$this->mod_lib->insert_log('Add admin_menu',$_SESSION['admin_id']);
				} else {
					$this->session->set_flashdata('msview', 'Thêm thất bại !');
				} 	
				
				redirect('back_end/properties');
			}else {
				$this->load->view('back_end/template_backend',$data);
			}
		}
		
	
		function edit($id = null){
			$data['title']			= "Sửa tin tức";
			$data['content_admin'] = 'back_end/properties/edit';
			$edit = $this->input->post('editForm');
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			$data['admin_properties'] = $this->db->get_where('properties',array( 'id' => $id ))->row_array();
			
			if(!empty($edit)){
				$where = array(
			  			'id' => $id 
			  		);
			  		
			  	$order = array(
			  			'id' => 'DESC' 
			  		);
			  		
				$data_value = array (
						'name'      		 => trim($this->input->post('name')),
						'description'        => $this->input->post('description'),
						'status'     		 => trim($this->input->post('status'))
				);
				
				if($this->mod_lib->update('properties',$where,$order,$data_value)){
					$this->session->set_flashdata('msview', 'Sửa thành công !');
				} else {
					$this->session->set_flashdata('msview', 'Sửa thất bại !');
				}
				
				redirect('back_end/properties');
			}else {		
				$this->load->view('back_end/template_backend',$data);		
			}
		}
			
		
		function delete() {
			$ids =  implode(",", $this->input->post('value_id'));

			if(!empty($ids)){
				if($this->mod_lib->deleteRecord('properties',$ids)){
					$this->session->set_flashdata('msview', 'Xóa thành công !');
				} else {
					$this->session->set_flashdata('msview', 'Xóa thất bại !');
				}
				
				$this->session->set_flashdata('msview', 'Xóa thành công !');
			} else {
				$this->session->set_flashdata('msview', 'Xóa thất bại !');
			}		
			redirect('back_end/properties');
		}

		function deleteId($id) {
			if(!empty($id)){
				if($this->mod_lib->deleteRecord('properties',$id)){
					$this->session->set_flashdata('msview', 'Xóa thành công !');
				} else {
					$this->session->set_flashdata('msview', 'Xóa thất bại !');
				}
				
				$this->session->set_flashdata('msview', 'Xóa thành công !');
			} else {
				$this->session->set_flashdata('msview', 'Xóa thất bại !');
			}		
			redirect('back_end/properties');
		}
		
	}
?>